package com.glowdayz.glowmee.bc;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.MultipartBox;
import com.glowdayz.common.PageHandler;
import com.glowdayz.common.Util;
import com.glowdayz.common.MultipartBox.FileInfo;
import com.glowdayz.glowmee.cmd.BrandCMD;
import com.glowdayz.glowmee.dao.BrandDAO;

public class ListBrandBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public ListBrandBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		String where = request.getParameter("where");
		if (where != null) {
			where = Util.getInstance().incoding(where);
		}
		
		String brandTitle = request.getParameter("brandTitle");
		String search = request.getParameter("search");
		
		if (brandTitle != null && !brandTitle.equals("")) {
			brandTitle = Util.getInstance().incoding(brandTitle);
			where = "(replace(brandTitle,' ','') LIKE '%" + brandTitle + "%' OR brandTitle like '%" + brandTitle + "%')";
		} else if (search != null && !search.equals("")) {
			int temp = Integer.parseInt(search);
			switch (temp) {
			case 1:
				where = "(brandTitle RLIKE '^(ㄱ|ㄲ)' OR (brandTitle >= '가' AND brandTitle < '나' ))";
				break;
			case 2:
				where = "(brandTitle RLIKE '^ㄴ' OR (brandTitle >= '나' AND brandTitle < '다'))";
				break;
			case 3:
				where = "(brandTitle RLIKE '^(ㄷ|ㄸ)' OR (brandTitle >= '다' AND brandTitle < '라'))";
				break;
			case 4:
				where = "(brandTitle RLIKE '^ㄹ' OR (brandTitle >= '라' AND brandTitle < '마'))";
				break;
			case 5:
				where = "(brandTitle RLIKE '^ㅁ' OR (brandTitle >= '마' AND brandTitle < '바'))";
				break;
			case 6:
				where = "(brandTitle RLIKE '^ㅂ' OR (brandTitle >= '바' AND brandTitle < '사'))";
				break;
			case 7:
				where = "(brandTitle RLIKE '^(ㅅ|ㅆ)' OR (brandTitle >= '사' AND brandTitle < '아'))";
				break;
			case 8:
				where = "(brandTitle RLIKE '^ㅇ' OR (brandTitle >= '아' AND brandTitle < '자'))";
				break;
			case 9:
				where = "(brandTitle RLIKE '^(ㅈ|ㅉ)' OR (brandTitle >= '자' AND brandTitle < '차'))";
				break;
			case 10:
				where = "(brandTitle RLIKE '^ㅊ' OR (brandTitle >= '차' AND brandTitle < '카'))";
				break;
			case 11:
				where = "(brandTitle RLIKE '^ㅋ' OR (brandTitle >= '카' AND brandTitle < '타'))";
				break;
			case 12:
				where = "(brandTitle RLIKE '^ㅌ' OR (brandTitle >= '타' AND brandTitle < '파'))";
				break;
			case 13:
				where = "(brandTitle RLIKE '^ㅍ' OR (brandTitle >= '파' AND brandTitle < '하'))";
				break;
			case 14:
				where = "(brandTitle RLIKE '^ㅎ' OR (brandTitle >= '하'))";
				break;
			case 15:
				where = "(brandTitle RLIKE '^[a-zA-Z]')";
				break;
			case 16:
				where = "(brandTitle RLIKE '^[0-9]')";
				break;
			default:
				where = "(brandTitle NOT RLIKE '^[ㄱ-힣a-zA-Z0-9]')";
				break;
			}
		}
		
		request.setAttribute("searchBrandTitle", brandTitle);
		
		String order = request.getParameter("order");
		request.setAttribute("sort", order);
		
		if (order != null && order.equals("isUnDisplay")) {
			order = "b.isDisplay ASC, b.idBrand DESC";
			request.setAttribute("order", "&order=isUnDisplay");
		} else if (order != null && order.equals("isDisplay")) {
			order = "b.isDisplay DESC, b.idBrand DESC";
			request.setAttribute("order", "&order=isDisplay");
		}
		
		if (order != null && order.equals("isUnCreate")) {
			order = "b.create_date ASC";
			request.setAttribute("order", "&order=isUnCreate");
		} else if (order != null && order.equals("isCreate")) {
			order = "b.create_date DESC";
			request.setAttribute("order", "&order=isCreate");
		}
		
		if (order != null && order.equals("isUnProduct")) {
			order = "count(p.idProduct) ASC, b.idBrand DESC";
			request.setAttribute("order", "&order=isUnProduct");
		} else if (order != null && order.equals("isProduct")) {
			order = "count(p.idProduct) DESC, b.idBrand DESC";
			request.setAttribute("order", "&order=isProduct");
		}
		
		if (order != null && order.equals("isUnLogo")) {
			order = "b.brandImg ASC, b.idBrand DESC";
			request.setAttribute("order", "&order=isUnLogo");
		} else if (order != null && order.equals("isLogo")) {
			order = "b.brandImg DESC, b.idBrand DESC";
			request.setAttribute("order", "&order=isLogo");
		}
		
		if (order != null && order.equals("isUnHomepage")) {
			order = "brandUrl ASC, b.idBrand DESC";
			request.setAttribute("order", "&order=isUnHomepage");
		} else if (order != null && order.equals("isHomepage")) {
			order = "brandUrl DESC, b.idBrand DESC";
			request.setAttribute("order", "&order=isHomepage");
		}
		
		if (order != null && order.equals("isUnFacebook")) {
			order = "brandFacebook ASC, b.idBrand DESC";
			request.setAttribute("order", "&order=isUnFacebook");
		} else if (order != null && order.equals("isFacebook")) {
			order = "brandFacebook DESC, b.idBrand DESC";
			request.setAttribute("order", "&order=isFacebook");
		}
		
		if (order != null && order.equals("isUnTwiter")) {
			order = "twitter ASC, b.idBrand DESC";
			request.setAttribute("order", "&order=isUnTwiter");
		} else if (order != null && order.equals("isTwiter")) {
			order = "twitter DESC, b.idBrand DESC";
			request.setAttribute("order", "&order=isTwiter");
		}
		
		if (order != null && order.equals("isUnKakao")) {
			order = "kakaotalk ASC, b.idBrand DESC";
			request.setAttribute("order", "&order=isUnKakao");
		} else if (order != null && order.equals("isKakao")) {
			order = "kakaotalk DESC, b.idBrand DESC";
			request.setAttribute("order", "&order=isKakao");
		}
		
		if (order != null && order.equals("isUnYouTube")) {
			order = "youTube ASC, b.idBrand DESC";
			request.setAttribute("order", "&order=isUnYouTube");
		} else if (order != null && order.equals("isYouTube")) {
			order = "youTube DESC, b.idBrand DESC";
			request.setAttribute("order", "&order=isYouTube");
		}
		
		String isDisplay_str = request.getParameter("isDisplay");
		String id_str = request.getParameter("id");
		if (id_str != null && isDisplay_str != null) {
			int id = Integer.parseInt(id_str);
			int isDisplay = Integer.parseInt(isDisplay_str);
			
			if (isDisplay == 1) {
				isDisplay = 0;
			} else if (isDisplay == 0) {
				isDisplay = 1;
			}
			
			BrandCMD cmd = new BrandCMD(id, isDisplay);
			try {
				BrandDAO.getInstance().unActivityBrand(cmd);
				request.setAttribute("alert", "<script type='text/javascript'>alert('처리가 완료되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		String pageCnt_str = request.getParameter("pageCnt");
		int pageCnt = 0;
		if (pageCnt_str == null) {
			pageCnt = 10;
		} else {
			pageCnt = Integer.parseInt(pageCnt_str);
		}
		
		try {
			if (where != null) request.setAttribute("where", "&where=" + URLEncoder.encode(where, "UTF-8"));
			
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("pageCnt", pageCnt);
			request.setAttribute("brand", BrandDAO.getInstance().getBrandList(page, pageCnt, order, where));
			if (where == null) {
				request.setAttribute("display", DBHandler.getInstance().getCount("brand", "idBrand", "isDisplay=1"));
				request.setAttribute("unDisplay", DBHandler.getInstance().getCount("brand", "idBrand", "isDisplay=0"));
			} else {
				request.setAttribute("display", DBHandler.getInstance().getCount("brand", "idBrand", "isDisplay=1 AND " + where));
				request.setAttribute("unDisplay", DBHandler.getInstance().getCount("brand", "idBrand", "isDisplay=0 AND " + where));
			}
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/brand/listBrand.jsp");
		dpr.forward(request, response);
	}
	
	/** 브랜드 추가이후 실행되는 메서드 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		MultipartBox mbox = MultipartBox.getInstance();
		FileInfo finfo = mbox.upload2(request);
		
		String brandTitle = mbox.getValue("brandTitle");
		String homepage = mbox.getValue("homepage");
		if (homepage.equals("")) {
			homepage = null;
		}
		String facebook = mbox.getValue("facebook");
		if (facebook.equals("")) {
			facebook = null;
		}
		String twitter = mbox.getValue("twitter");
		if (twitter.equals("")) {
			twitter = null;
		}
		String kakaotalk = mbox.getValue("kakaotalk");
		if (kakaotalk.equals("")) {
			kakaotalk = null;
		}
		String youTube = mbox.getValue("youTube");
		if (youTube.equals("")) {
			youTube = null;
		}
		String tel1 = mbox.getValue("tel1");
		String tel2 = mbox.getValue("tel2");
		String tel3 = mbox.getValue("tel3");
		String tel = tel1 + "-" + tel2 + "-" + tel3;
		
		try {
			BrandDAO.getInstance().insertBrand(new BrandCMD(brandTitle, homepage, facebook, twitter, kakaotalk, youTube, tel, finfo));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			mbox.deleteFile(finfo);
			e.printStackTrace();
		}
		
		try {
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("brand", BrandDAO.getInstance().getBrandList(1, 10, null, null));
			request.setAttribute("display", DBHandler.getInstance().getCount("brand", "idBrand", "isDisplay='1'"));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount("brand", "idBrand", "isDisplay='0'"));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/brand/listBrand.jsp");
		dpr.forward(request, response);
	}

}